﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using BO = Viewer.BusinessObject.BusinessEntities;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomNguoiDung : BusinessBase
    {
        CommonCode common = new CommonCode();
        public string TenLoaiNhomNguoiDung { get; set; }
        public List<QUNhomNguoiDung> GetAll()
        {
            if (HttpContext.Current.User.Identity.Name.ToLower() == "admin")
            {
                List<QUNhomNguoiDung> lst = this.Context.QUNhomNguoiDungs.ToList();
                //foreach (QUNhomNguoiDung item in lst)
                //{
                //    item.TenLoaiNhomNguoiDung = common.LoadCommonCodeById(item.LoaiNhomNguoiDungID);
                //}
                return lst;
            }
            else
            {               
                List<QUNguoiDung> nguoiDung = new BO.QUNguoiDung().SearchNguoiDung(HttpContext.Current.User.Identity.Name.ToLower());

                var lstNhomNguoiDung = (from nnd_nd1 in this.Context.QUNhomNguoiDung_NguoiDungs
                                        where nnd_nd1.NguoiDungID == nguoiDung[0].NguoiDungID
                                        select nnd_nd1.NhomNguoiDungID).Distinct();
                List<string> nhomNguoiDung = new List<string>();

                foreach (var item in lstNhomNguoiDung)
                {
                    nhomNguoiDung.Add(item.ToString());
                }

                var lstNhomNguoiDungFilter = (from nd in this.Context.QUNguoiDungs
                                    join nnd_nd in this.Context.QUNhomNguoiDung_NguoiDungs on nd.NguoiDungID equals nnd_nd.NguoiDungID
                                    join nnd in this.Context.QUNhomNguoiDungs
                                    on nnd_nd.NhomNguoiDungID equals nnd.NhomNguoiDungID
                                    where nhomNguoiDung.Contains(nnd.NhomNguoiDungID.ToString())
                                    select nnd).ToList();
                //foreach (QUNhomNguoiDung item in lstNhomNguoiDungFilter)
                //{
                //    item.TenLoaiNhomNguoiDung = common.LoadCommonCodeById(item.LoaiNhomNguoiDungID);
                //}
                return lstNhomNguoiDungFilter;
            }
        }
        public QUNhomNguoiDung GetByID(int id)
        {
            return this.Context.QUNhomNguoiDungs.Where(c => c.NhomNguoiDungID == id).FirstOrDefault();
        }

        /// <summary>
        /// Ham nay lay tat ca nguoi dung thuoc nhom nguoi dung nay
        /// </summary>
        /// <returns></returns>
        public List<QUNguoiDung> GetNguoiDung()
        {
            return (from m in this.QUNhomNguoiDung_NguoiDungs
                          select m.QUNguoiDung).ToList();

        }

        public void Save()
        {
            if (this.NhomNguoiDungID == 0)
            {
                //add new
                this.Context.QUNhomNguoiDungs.InsertOnSubmit(this);
                this.Context.SubmitChanges();
            }
            else
            { 
                //update
                var oItem = GetByID(this.NhomNguoiDungID);
                oItem.TenNhomNguoiDung = this.TenNhomNguoiDung;
                //oItem.LoaiNhomNguoiDungID = this.LoaiNhomNguoiDungID;
                oItem.GhiChu = this.GhiChu;
                //Delete existing item
                foreach (var item in oItem.QUNhomNguoiDung_NguoiDungs)
                {
                    this.Context.QUNhomNguoiDung_NguoiDungs.DeleteOnSubmit(item);
                }
                //add new item
                QUNhomNguoiDung_NguoiDung oNhomNguoiDung_NguoiDung;
                foreach (var item in this.QUNhomNguoiDung_NguoiDungs)
                {
                    oNhomNguoiDung_NguoiDung = new QUNhomNguoiDung_NguoiDung();
                    oNhomNguoiDung_NguoiDung.NguoiDungID = item.NguoiDungID;
                    //oNhomNguoiDung_NguoiDung.NhomNguoiDungID = item.NhomNguoiDungID;

                    oItem.QUNhomNguoiDung_NguoiDungs.Add(oNhomNguoiDung_NguoiDung);
                }
                this.Context.SubmitChanges();
            }
        }

        public void Delete()
        {
            try
            {
                var oItem = GetByID(this.NhomNguoiDungID);
                foreach (var item in oItem.QUNhomNguoiDung_NguoiDungs)
                {
                    this.Context.QUNhomNguoiDung_NguoiDungs.DeleteOnSubmit(item);
                }
                this.Context.QUNhomNguoiDungs.DeleteOnSubmit(oItem);
                this.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public List<QUNhomNguoiDung_NguoiDung> QUNhomNguoiDung_NguoiDungs
        //{ 
            
        //}

    }
}